﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;

using Kwan.Data;
using BG.Data;

namespace BG.Service
{
	public class OrderQuery
	{

		public static Expression MakeSearchWhere(NameValueCollection ps)
		{
			Expression exp = new Expression();

            string propName = "EntityID";
            if (!string.IsNullOrEmpty(ps[propName]))
                exp &= TOrder.entityID == int.Parse(ps[propName]);

            propName = "cardNO";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                exp &= TOrder.cardNO == ps[propName];
            }
            propName = "checkInDate";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                exp &= TOrder.checkInDate == DateTime.Parse(ps[propName]);
            }
            propName = "checkInPrice";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                exp &= TOrder.checkInPrice == double.Parse(ps[propName]);
            }
            propName = "checkInRoomPrice";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                exp &= TOrder.checkInRoomPrice == double.Parse(ps[propName]);
            }
            propName = "checkInType";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                exp &= TOrder.checkInType == int.Parse(ps[propName]);
            }
            propName = "customerID";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                exp &= TOrder.customerID == int.Parse(ps[propName]);
            }
            propName = "days";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                exp &= TOrder.days == int.Parse(ps[propName]);
            }
            propName = "deposit";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                exp &= TOrder.deposit == double.Parse(ps[propName]);
            }
            propName = "disCount";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                exp &= TOrder.disCount == int.Parse(ps[propName]);
            }
            propName = "finalRoomPrice";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                exp &= TOrder.finalRoomPrice == double.Parse(ps[propName]);
            }
            propName = "memo";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                exp &= TOrder.memo == ps[propName];
            }
            propName = "pid";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                exp &= TOrder.pid == int.Parse(ps[propName]);
            }
            propName = "roomID";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                exp &= TOrder.roomID == int.Parse(ps[propName]);
            }
            propName = "status";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                exp &= TOrder.status == int.Parse(ps[propName]);
            }

            propName = "tag";
            if (!string.IsNullOrEmpty(ps[propName]))
            {
                string val = ps[propName];
                string[] vals = val.Split(',');
                if (!string.IsNullOrEmpty(vals[0]))
                {
                    exp &= VOrder.roomNO == vals[0];
                }
                bool startNull = string.IsNullOrEmpty(vals[1]);
                bool endNull = string.IsNullOrEmpty(vals[2]);
                if (!startNull && !endNull)
                {
                    DateTime dt1 = DateTime.Parse(vals[1] + " 6:00:00");
                    DateTime dt2 = DateTime.Parse(vals[2] + " 6:00:00").AddDays(1);
                    exp &= VOrder.collectDate.Between(dt1, dt2);
                }
                else if (startNull && !endNull)
                {
                    exp &= VOrder.collectDate <= DateTime.Parse(vals[2] + " 6:00:00").AddDays(1);
                }
                else if (!startNull && endNull)
                {
                    exp &= VOrder.collectDate >= DateTime.Parse(vals[1] + " 6:00:00");
                }
            }
			return exp;
		}
	}
}